// SPDX-FileCopyrightText: 2024 CitruS Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

@file:Suppress("EXTENSION_SHADOWED_BY_MEMBER")

package com.fanwj.citrus.features.input.model

import com.fanwj.citrus.aidl.IBackend

typealias PlayerInput = IBackend.PlayerInput

// It's recommended to use the generated equals() and hashCode() methods
// when using arrays in a data class
fun PlayerInput.equals(other: Any?): Boolean {
    if (this === other) return true
    if (javaClass != other?.javaClass) return false

    other as PlayerInput

    if (connected != other.connected) return false
    if (!buttons.contentEquals(other.buttons)) return false
    if (!analogs.contentEquals(other.analogs)) return false
    if (!motions.contentEquals(other.motions)) return false
    if (vibration_enabled != other.vibration_enabled) return false
    if (vibration_strength != other.vibration_strength) return false
    if (body_color_left != other.body_color_left) return false
    if (body_color_right != other.body_color_right) return false
    if (button_color_left != other.button_color_left) return false
    if (button_color_right != other.button_color_right) return false
    if (profile_name != other.profile_name) return false
    return use_system_vibrator == other.use_system_vibrator
}

fun PlayerInput.hashCode(): Int {
    var result = connected.hashCode()
    result = 31 * result + buttons.contentHashCode()
    result = 31 * result + analogs.contentHashCode()
    result = 31 * result + motions.contentHashCode()
    result = 31 * result + vibration_enabled.hashCode()
    result = 31 * result + vibration_strength
    result = 31 * result + body_color_left.hashCode()
    result = 31 * result + body_color_right.hashCode()
    result = 31 * result + button_color_left.hashCode()
    result = 31 * result + button_color_right.hashCode()
    result = 31 * result + profile_name.hashCode()
    result = 31 * result + use_system_vibrator.hashCode()
    return result
}

fun PlayerInput.hasMapping(): Boolean {
    var hasMapping = false
    buttons.forEach {
        if (it != "[empty]" && it.isNotEmpty()) {
            hasMapping = true
        }
    }
    analogs.forEach {
        if (it != "[empty]" && it.isNotEmpty()) {
            hasMapping = true
        }
    }
    motions.forEach {
        if (it != "[empty]" && it.isNotEmpty()) {
            hasMapping = true
        }
    }
    return hasMapping
}